//
//  UIView+SCCreate.m
//  Shuaiche
//
//  Created by dangshuai on 2019/3/11.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "UIView+SCCreate.h"
#import "UIImage+Color.h"
#import <Masonry.h>

@implementation UIView (SCCreate)

+ (UIButton *)createMainTintButtonWithTitle:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = SC_FONT(15);
    [button setTitleColor:SC_TextBlack_Primary forState:0];
    [button setTitleColor:SC_TextBlack_Third forState:UIControlStateDisabled];
    [button setTitle:title forState:0];
    [button setBackgroundImage:[UIImage imageWithColor:SC_Color_Tint] forState:0];
    [button setBackgroundImage:[UIImage imageWithColor:SC_RGB(242,213,0)] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageWithColor:SC_RGB(228,228,228)] forState:UIControlStateDisabled];
    return button;
}


- (void)maskLayerWithCornerRadius:(CGFloat)r {
    self.layer.cornerRadius = r;
    self.layer.masksToBounds = YES;
}

- (void)createBottomButtons:(NSArray *)titles handle:(void(^)(NSInteger idx))block {
    
    UIStackView *stackView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, 45)];
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.axis = UILayoutConstraintAxisHorizontal;
    
    CGFloat left = 0;
    if (kSCScreenBottomOffset > 0) {
        left = 15;
        self.layer.shadowColor = SC_RGB(204, 204, 204).CGColor;
        self.layer.shadowOffset = CGSizeZero;
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 2;
        self.layer.cornerRadius = 4;
        
        UIView *contentView = [[UIView alloc] init];
        contentView.frame = CGRectMake(0, 0, kSCScreenWidth - 30, 45);
        contentView.layer.masksToBounds = YES;
        contentView.layer.cornerRadius = 4;
        [self addSubview:contentView];
        stackView.frame = CGRectMake(0, 0, kSCScreenWidth - 30, 45);
        [contentView addSubview:stackView];
    } else {
        CALayer *layer = [CALayer layer];
        [self.layer addSublayer:layer];
        layer.frame = CGRectMake(0, 0, kSCScreenWidth, 0.5);
        layer.backgroundColor = SC_Color_Separator.CGColor;
        [self addSubview:stackView];
    }
    
    self.frame = CGRectMake(left, kSCScreenHeight - 45 - kSCScreenBottomOffset, kSCScreenHeight - 2 * left, 45);
    SEL selectorBlock = [self selectorBlock:block];
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = SC_FONT(15);
        [button setTitleColor:SC_TextBlack_Primary forState:0];
        [button setTitle:titles[i] forState:0];
        if (i > 0) {
            [button setBackgroundColor:SC_Color_Tint];
        } else {
            [button setBackgroundColor:[UIColor whiteColor]];
        }
        button.tag = i + 1000;
        [button addTarget:self action:selectorBlock forControlEvents:UIControlEventTouchUpInside];
        [stackView addArrangedSubview:button];
    }
}

+ (UIButton *)createBottomButton:(NSString *)title {
    
    
    return nil;
}


- (void)setBackgroundColor:(UIColor *)backgroundColor forIndex:(NSInteger)idx {
    
    
}

- (SEL)selectorBlock:(void (^)(NSInteger idx))block {
    NSString *selName = [NSString stringWithFormat:@"sel_%p:", block];
    SEL sel = NSSelectorFromString(selName);
    class_addMethod([self class], sel, (IMP)selectorImp, "v@:@");
    objc_setAssociatedObject(self, sel, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    return sel;
}

static void selectorImp(id self, SEL _cmd, UIButton *arg) {
    void (^block)(NSInteger idx) = objc_getAssociatedObject(self, _cmd);
    if (block) block(arg.tag-1000);
}

//static SEL selBlock(id _self_, id block) {
//    NSString *selName = [NSString stringWithFormat:@"sel_%p:", block];
//    SEL sel = NSSelectorFromString(selName);
//    class_addMethod([_self_ class], sel, (IMP)selectorImp, "v@:@");
//    objc_setAssociatedObject(_self_, sel, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
//    return sel;
//}

+ (UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)color {
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.textColor = color;
    return label;
}

- (UIView *)SCsnapshotView {
    if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]) {
        return [self snapshotViewAfterScreenUpdates:YES];
    } else {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0f);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return [[UIImageView alloc] initWithImage:image];
    }
}

@end
